<template>
  <div>
    <!-- 搜索 -->
    <!-- 技术支持报告审核 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="100" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :fetchapi="fetchApi"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="exportFileBtn()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleEdit" :title="'技术支持报告审核编辑'" destroyOnClose width="100%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <EditForm @close="closeEdit" @save="saveEdit" :rowData="rowData"></EditForm>
    </Drawer>
    <Drawer :visible.sync="visibleDetail" :title="'技术支持报告审核明细'" destroyOnClose width="100%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DetailForm @close="closeDetail" :rowData="rowData"></DetailForm>
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ModelVehicle @close="close" @save="save"></ModelVehicle>
    </BaseDialog>
    <BaseDialog :visible.sync="visibleVehicle" :title="'经销商选择'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <vehicle @close="closeVehicle" @save="saveVehicle"></vehicle>
    </BaseDialog>
  </div>
</template>
<script>
import EditForm from './EditForm.vue';
import DetailForm from './DetailForm.vue';
import vehicle from '@/pages/claimDemage/tools/DealerList.vue';
import ModelVehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { getListByPage, getListForExport,exporturl } from '@/api/technicalReportManagement/technicalSupportManagement_OEM/onSitePersonnelRecords/index';
//字典
import { getPartType } from '@/api/dictionary/index';
import { exportExcelFile } from '@/utils/index';
import moment from "moment";
import { getAppId } from '@/assets/js/auth';

export default {
  // mixins: [dictionary],
  components: {
    EditForm,
    DetailForm,
    ModelVehicle,
    vehicle
  },
  data() {
    return {
      dataList: [],
      visible: false,
      collapse: true,
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      fetchApi: getListByPage,
      fetchParams: {
        techStatusStrs: '55521004, 55521005',
        dealType: 55541002
      }, //ipa 接口
      selectedRows: [], //选中经销商
      visibleVehicle: false,
      visibleOrders: false,
      visibleEdit: false,
      visibleDetail: false,
      rowData: null
    };
  },
  created() {
    this.fetchParams.beginApplyDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endApplyDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.chuqi();
  },
  methods: {
    async chuqi() {
      //状态
      const res1 = await getPartType({ type: 5595 });
      if (res1.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'auditStatus').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'auditStatus').itemList = res1.data.map(y => ({
          text: y.codeCnDesc,
          value: y.codeId
        }));
      }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    chooseOrderNo() {
      this.visible = true;
    },
   async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000256' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { techStatusStrs: '55521004,55521005', dealType: 55541002 });
      // obj.beginApplyDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[0];
      // obj.endApplyDate = this.topFilterInit.find(x => x.fieldName == "beginApplyDate|endApplyDate").initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '技术支持报告');
      // });
    },
    edit(val) {
      this.rowData = val;
      console.log('编辑ID', val);
      this.visibleEdit = true;
    },
    closeEdit() {
      this.visibleEdit = false;
    },
    closeDetail() {
      this.visibleDetail = false;
    },
    closeOrders() {
      this.visibleOrders = false;
    },
    saveEdit() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveOrders() {
      this.visibleOrders = false;
      this.fetchParams = { ...this.fetchParams };
    },
    close() {
      this.visible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    save(obj) {
      this.visible = false;
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
    },
    VehicleTurnOn() {
      this.visibleVehicle = true;
    },
    DetailTurnOn(val) {
      this.rowData = val;
      this.visibleDetail = true;
    },
    closeVehicle() {
      this.visibleVehicle = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveVehicle(obj) {
      this.visibleVehicle = false;
      this.topFilterInit.find(x => x.fieldName == 'dealerCode').initialValue = obj[0].dealerCode;
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginApplyDate) {
        this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
        return
      }
      if(!val.endApplyDate) {
        this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endApplyDate).format('YYYY-MM-DD')).diff(moment(val.beginApplyDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endApplyDate).format('YYYY-MM-DD')).diff(moment(val.beginApplyDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      }
      // this.fetchParams = Object.assign({},val,{ techStatus: 55521002 });
      this.fetchParams = Object.assign({}, val, { techStatusStrs: '55521004, 55521005', dealType: 55541002 });

      console.log(val);
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入经销商代码...',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.VehicleTurnOn()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '技术报告单号', fieldName: 'techNo', placeholder: '请输入技术报告单号...', initialValue: '' },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'auditStatus',
          placeholder: '请选择状态...',
          initialValue: '',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'beginApplyDate|endApplyDate',
          placeholder: '请输入',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请输入车型...',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入VIN...', initialValue: '' },
        { type: 'INPUT', label: '接单人', fieldName: 'takerName', placeholder: '请输入接单人...', initialValue: '' }
      ];
    },

    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.DetailTurnOn(props.row)}>
                  查看
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '状态', dataIndex: 'auditStatusStr', sorter: true },
        { title: '经销商代码', dataIndex: 'dealerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'dealerName', sorter: true },
        { title: '所属区域', dataIndex: 'belongArea', sorter: true },
        { title: '技术报告单号', dataIndex: 'techNo', sorter: true },
        { title: '是否首次咨询', dataIndex: 'ifFirstStr', sorter: true },
        { title: '处理方式', dataIndex: 'dealTypeStr', sorter: true },
        { title: '经销商联系人', dataIndex: 'dcPerson', sorter: true },
        { title: '经销商联系人电话', dataIndex: 'dcTel', sorter: true },
        { title: '首次提交时间', dataIndex: 'firstDate', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '车型', dataIndex: 'modelName', sorter: true },
        { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
        { title: '生产日期', dataIndex: 'prodDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '销售日期', dataIndex: 'saleDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '车牌号', dataIndex: 'licenseNo', sorter: true },
        { title: '受理类型', dataIndex: 'acceptTypeStr', sorter: true },
        { title: '现场处理人', dataIndex: 'siteDealerStr', sorter: true }
        // { title: '提交时间', dataIndex: 'applyDate', sorter: true ,dateFormat: 'yyyy-MM-dd'},
        // { title: '评价结果', dataIndex: 'evaluateResultStr', sorter: true },
        // { title: '关单时间', dataIndex: 'closeDate', sorter: true ,dateFormat: 'yyyy-MM-dd'}
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
